package com.zhangxun.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.zhangxun.constant.QueueConstant;
import com.zhangxun.domain.TimeOutDto;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderTimeOutListener {


    /**
     * 监听死信队列，删除订单，扣减数据库和es库存
     * @param message
     * @param channel
     */
    @RabbitListener(queues = QueueConstant.DEAD_ORDER_QUEUE)
    public void listen(Message message, Channel channel){
        String str = new String(message.getBody());

        TimeOutDto timeOutDto = JSON.parseObject(str, TimeOutDto.class);

        System.out.println(timeOutDto.getOrderNumber());

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
